//
//  Myuser.m
//  项目三
//
//  Created by rimi1 on 2016/12/9.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "MyUser.h"

static MyUser *_user = nil;

@implementation MyUser

+ (instancetype)sharedUser {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 解档
        [MyUser readInfo];
    });
    
    return _user;
}

+ (void)initUserWithObj:(AVObject *)obj {
    _user.isSignIn = YES;
    _user.userId = obj.objectId;
    _user.account = obj[@"account"];
    _user.password = obj[@"password"];
    _user.name = obj[@"name"];
    _user.sex = obj[@"sex"];
    _user.age = obj[@"age"];
    _user.headImage = obj[@"headImage"];
    _user.fansCount = obj[@"fansCount"];
    _user.concernCount = obj[@"concernCount"];
    _user.myDynamicCount = obj[@"myDynamicCount"];
    // 归档
    [MyUser save];
}

+ (void)signOut {
    _user.isSignIn = NO;
    _user.userId = nil;
    _user.account = nil;
    _user.password = nil;
    _user.name = nil;
    _user.sex = nil;
    _user.age = nil;
    _user.headImage = nil;
    _user.fansCount = nil;
    _user.concernCount = nil;
    _user.myDynamicCount = nil;
    
    [MyUser save];
}

#pragma mark - 归解档
// 解档
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        self.isSignIn = [[aDecoder decodeObjectForKey:@"isSignIn"] boolValue];
        self.userId = [aDecoder decodeObjectForKey:@"userId"];
        self.account = [aDecoder decodeObjectForKey:@"account"];
        self.password = [aDecoder decodeObjectForKey:@"password"];
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.sex = [aDecoder decodeObjectForKey:@"sex"];
        self.age = [aDecoder decodeObjectForKey:@"age"];
        self.headImage = [aDecoder decodeObjectForKey:@"headImage"];
        self.fansCount = [aDecoder decodeObjectForKey:@"fansCount"];
        self.concernCount = [aDecoder decodeObjectForKey:@"concernCount"];
        self.myDynamicCount = [aDecoder decodeObjectForKey:@"myDynamicCount"];
    }
    return self;
}
// 归档
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:@(self.isSignIn) forKey:@"isSignIn"];
    [aCoder encodeObject:self.userId forKey:@"userId"];
    [aCoder encodeObject:self.account forKey:@"account"];
    [aCoder encodeObject:self.password forKey:@"password"];
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.sex forKey:@"sex"];
    [aCoder encodeObject:self.age forKey:@"age"];
    [aCoder encodeObject:self.headImage forKey:@"headImage"];
    [aCoder encodeObject:self.fansCount forKey:@"fansCount"];
    [aCoder encodeObject:self.concernCount forKey:@"concernCount"];
    [aCoder encodeObject:self.myDynamicCount forKey:@"myDynamicCount"];
}

+ (void)save {
    // 文件名称和后缀都是随意 /xxxx.xxx
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingString:@"/userInfo.dat"];
    // 归档
    [NSKeyedArchiver archiveRootObject:_user toFile:path];
}

+ (void)readInfo {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingString:@"/userInfo.dat"];
    // 解档
    _user = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    // 当用户不存在，就是记录是没账号登录得
    if (!_user) {
        _user = [[MyUser alloc] init];
    }
    
}

@end













